package controller;

/**
 * Created by kang on 1/14/15.
 */


import view.*;
import model.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TheController {

	private TheFrame theView = new TheFrame();

	private TheModel theModel = new TheModel();


    public TheController () {

    }


	public TheController(final TheFrame theFrame, final TheModel theData) {
		this.theView = theFrame;

		this.theModel = theData;


		this.theView.addCalculateListener(new CalculateListener());
	}


    public int  addTwoNumbers(int firstNumber, int secondNumber){

        return  firstNumber + secondNumber;

    }

	class CalculateListener implements ActionListener{

					        public void actionPerformed(ActionEvent e) {

				            int firstNumber, secondNumber = 0;

				            // Surround interactions with the view with
				            // a try block in case numbers weren't
				            // properly entered

			            try{

			                firstNumber = theView.getFirstNumber();
				            secondNumber = theView.getSecondNumber();


				            theModel.setCalculatedValue(addTwoNumbers(firstNumber, secondNumber));

					        theView.setCalcSolution(theModel.getCalculationValue());

					    }

				            catch(NumberFormatException ex){

					                System.out.println(ex);

					                theView.displayErrorMessage("You Need to Enter 2 Integers");

				            }

				        }

					    }



}